﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Seller
{
    public partial class mfReleasePost : msBsc.Cls.mBPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void publishingForum()
        {
            string forumTitle = this.GetParamHtValue("forumTitle");
            string forumInfo = this.GetParamHtValue("forumInfo");
            string publishId = msBsc.SvRef.UserInfo.Operator;
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");
            string destId = this.GetParamHtValue("desId");
            string custKeyword = this.GetParamHtValue("custKeyword");

            if (string.IsNullOrEmpty(forumTitle))
            {
                this.Alert("标题不能为空");
                return;
            }
            if (string.IsNullOrEmpty(countryId)
                || string.IsNullOrEmpty(cityId))
            {
                this.Alert("请至少选择两级地区信息");
                return;
            }
            if (string.IsNullOrEmpty(custKeyword))
            {
                this.Alert("请输入关键字，让你的推荐更易被查找");
                return;
            }
            if (custKeyword.Length > 125)
            {
                this.Alert("关键字过长，不能超过120个字符");
                return;
            }
            if (string.IsNullOrEmpty(forumInfo))
            {
                this.Alert("发布内容不能为空");
                return;
            }

            if (string.IsNullOrEmpty(publishId))
            {
                this.ExecJScript(@"window.location.href = ""/""");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizSourcePosts sourcePosts = new AppLogic.DataAlc.CBizSourcePosts())
            {
                string[] ret = sourcePosts.AddSourcePosts(publishId, forumTitle, forumInfo, countryId, cityId, destId, custKeyword);
                if (ret[0] == "OK")
                {
                    this.ExecJScript(@"alert('发布成功');$('#title').val('');
                        $('#forumInfo').val();
                        KindEditor.html('#forumInfo', '');");
                }
                else
                {
                    this.Alert(ret[1]);
                }
            }
        }
        /// <summary>   
        /// 取得HTML中所有图片的 URL。   
        /// </summary>   
        /// <param name="sHtmlText">HTML代码</param>   
        /// <returns>图片的URL列表</returns>   
        private string GetHtmlImageUrlList(string sHtmlText)
        {
            // 定义正则表达式用来匹配 img 标签   
            Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);

            // 搜索匹配的字符串   
            MatchCollection matches = regImg.Matches(sHtmlText);
            if (matches.Count < 1)
            {
                return sHtmlText;
            }
            string tempurl = "";
            // 取得匹配项列表   
            foreach (Match match in matches)
            {
                //上传到服务器
                string str64 = match.Groups["imgUrl"].Value;
                string postFileNme = this.GetParamHtValue("postFileNme");
                try
                {
                    using (msBsc.AppLogic.DataAlc.CBizSysFile sysFile = new AppLogic.DataAlc.CBizSysFile())
                    {
                        tempurl = sysFile.SaveBase64ImgServer(str64, "/UpLoad/comments/");
                    }
                }
                catch (Exception e)
                {
                    //this.Alert(e.Message);
                    //this.ExecJScript("$('#uploadImg').val('上 传');");
                }
                //替换图片路径
                string temp = "src=\"" + match.Groups["imgUrl"].Value + "\"";
                sHtmlText = Regex.Replace(sHtmlText, temp, "src=\"" + tempurl + "\"");

            }

            return sHtmlText;
        }

        public override void DoAction()
        {
            if (this.strAction == "published")
            {
                publishingForum();
            }
        }
    }
}